<?php
class Administrator_AdvController extends Nguyen_Controller_Action{
	public function init(){
		$this->loadTemplate('administrator');
	}
	public function indexAction(){
		
	}
	public function positionAction(){
		$model = new Administrator_Model_Adv();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id','');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/adv/position?task=new');
			}
			
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');	
				$width = $this->_request->getParam('width','');
				$height = $this->_request->getParam('height','');				
				if($task == 'new'){
					
					$model->insertAdv_position($name, $width, $height, '1');
				}
				if($task == 'edit'){
					
					$model->updateAdv_position($id, $name, $width, $height);
					
				}
				$this->_redirect('/administrator/adv/position');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllAdv_position() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/adv/position?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				
				foreach ($model->GetAllAdv_position() as $value) {
					if($this->_request->getParam($value['id']))
					{					
							$model->delAdv_position($value['id']);						
					}
				}
				$this->_redirect('/administrator/adv/position ');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/adv/position');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin thể loại',true);
	
	}
	
	public function detailsAction(){
		$model = new Administrator_Model_Adv();
		
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/adv/details?task=new');
			}
			
			if ($this->_request->getParam('btsave')){
				$posi = $model->GetAdv_positionById($model->GetPositionMinid());
				$name = $this->_request->getParam('name','');	
				$position = $this->_request->getParam('position',$posi['name']);
				$ordering  = $this->_request->getParam('ordering','');
				if($ordering == '') $ordering = 1;
				$content = $this->_request->getParam('content','');
				$type = $this->_request->getParam('type','');
				$link = $this->_request->getParam('link','');						
				if($task == 'new'){
			
					$model->insertAdv_details($name, $position, $ordering, $type, $content, $link, '1');
				}
				if($task == 'edit'){
				
					$model->updateAdv_details($id, $name, $position, $ordering, $type, $content, $link);
					
				}
				$this->_redirect('/administrator/adv/details');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllAdv_details() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/adv/details?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				
				foreach ($model->GetAllAdv_details() as $value) {
					if($this->_request->getParam($value['id']))
					{					
							$model->delAdv_details($value['id']);						
					}
				}
				$this->_redirect('/administrator/adv/details ');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/adv/details');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin nhà quảng cáo',true);
	}
}